package to52.android.adapters;

import to52.android.screens.R;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fr.utbm.Patient;
import fr.utbm.Sensor;

public class PatientDetailsAdapter extends BaseAdapter {
	
    private static class ViewHolder {
    	ImageView image;
        TextView name;
        TextView value;
    }
	
    private Patient patient;
	private Context mContext;
    private LayoutInflater mInflater;
    private ViewHolder holder;
	
    public int getCount() {
        return patient.getSensors().size();
    }

    public Object getItem(int position) {
        return patient.getSensors().get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    
    
    public PatientDetailsAdapter(Context c, Patient patient) {
        mContext = c;
        mInflater = LayoutInflater.from(mContext);
        this.patient = patient;
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.patient_details_row, null);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.patient_details_row_label);
            holder.image = (ImageView) convertView.findViewById(R.id.patient_details_icon);
            holder.value = (TextView) convertView.findViewById(R.id.patient_details_row_value);
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Sensor s = patient.getSensors().get(position);

        holder.name.setText(s.getType().getLabel());
        
        if (s.getType().getLabel().startsWith("Poids")) {
        	holder.image.setImageBitmap(((BitmapDrawable)mContext.getResources().getDrawable(R.drawable.poids)).getBitmap());
        }
        else if (s.getType().getLabel().startsWith("Pression")) {
        	holder.image.setImageBitmap(((BitmapDrawable)mContext.getResources().getDrawable(R.drawable.tension)).getBitmap());
        }
        else if (s.getType().getLabel().startsWith("Saturation")) {
        	holder.image.setImageBitmap(((BitmapDrawable)mContext.getResources().getDrawable(R.drawable.saturation)).getBitmap());
        }
        
 
        if ( ! s.getValues().isEmpty() ) {
        	holder.value.setText(s.getValues().get(s.getValues().size() - 1).getValue() + " " + s.getType().getUnite());
        }
     
        return convertView;
	}
	
}

